﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using System.Speech.Synthesis;

namespace BoxDiary
{
    public partial class ReadDiary : UserControl
    {
        public ReadDiary()
        {
            InitializeComponent();
        }

        const string udu = "https://forms.office.com/Pages/ResponsePage.aspx?id=TVmVBDZlMkKXdiu4emLqnFiH7ZKSoYBHnWA9UQhneWlUNk9CUjU4VFZQMzkzRlcxQ1E5UUZNTzlNUi4u";

        private void ReadDiary_Load(object sender, EventArgs e)
        {
            //将所有日记加载到缓冲区
            string path = $@"{Application.StartupPath}\Diary";
            List<string> allfile = new List<string>();
            foreach (string filename in Directory.GetFiles(path))
            {
                FileInfo file = new FileInfo(filename);
                allfile.Add(file.Name);
            }

            foreach (string a in allfile)
            {
                comboBox1.Items.Add(a.Split('.')[0]);
            }

            textBox1.UseSystemPasswordChar = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            richTextBox1.LoadFile(Application.StartupPath +
                $"\\Diary\\{comboBox1.Text}.rtf", RichTextBoxStreamType.RichText);
            string dResult = richTextBox1.Text;
            string result = richTextBox1.Text.Split(';')[1];
            try
            {
                string txt = AES.EncryptNew.AESDecrypt(result, textBox1.Text);
                richTextBox1.Text = txt;

                if (MessageBox.Show
                    ("是否为您朗读日记？","Speecher",
                    MessageBoxButtons.YesNo,MessageBoxIcon.Question) 
                    == DialogResult.Yes
                    )
                {
                    var speech = new SpeechSynthesizer();
                    speech.SpeakAsync(richTextBox1.Text);
                }
            }
            catch
            {
                richTextBox1.Text = "";
                if (MessageBox.Show("秘钥错误！您是否忘记了秘钥？",
                    "Failed To Load File", 
                    MessageBoxButtons.YesNo, 
                    MessageBoxIcon.Error)== 
                    DialogResult.Yes)
                {
                    MessageBox.Show($"请上传您无法打开的日记。我们会对其进行修复。\n我们已经自动将您的日记内容拷贝到剪切板，直接粘贴日记内容即可。","R System",MessageBoxButtons.OK,MessageBoxIcon.Information);
                    Clipboard.SetDataObject(dResult);
                    System.Diagnostics.Process.Start(udu);
                }
            }
        }
    }
}
